import javax.print.attribute.standard.MediaSize.Other
import org.apache.spark.{SparkConf, SparkContext}

object SecondarySort {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("SecondarySort").setMaster("local")
    conf.set("spark.testing.memory", "2147480000")
    val sc = new SparkContext(conf)
    val lines = sc.textFile("src/secondarySort", 1)
    val secondarySort = lines.filter(line => line.length()>0)
      .map(line => (new SecondarySortKey(line.split(" ")(0).toInt, line.split(" ")(1).toInt), line))
    val sorded = secondarySort.sortByKey(false).map(x => (x._2.split(" ")(0), x._2.split(" ")(1)))
    sorded.saveAsTextFile("src/secondarySort/result")
  }

}


class SecondarySortKey(val first:Int, val second:Int) extends Ordered[SecondarySortKey] with Serializable{
  override def compare(other: SecondarySortKey): Int = {
    if(first != other.first){
      first - other.first
    }else{
      second - other.second
    }
  }
}
